package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingBaseTaxRateCategoryEntity;
import com.ly.heart.domain.KingBaseTaxRateSystemEntity;
import com.ly.heart.mapper.KingBaseTaxRateMapper;
import com.ly.heart.service.IKingBaseTaxRateCategoryService;
import com.ly.heart.service.IKingBaseTaxRateService;
import com.ly.heart.service.IKingBaseTaxRateSystemService;
import com.ly.heart.util.CurrentUserUtil;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.KingBaseTaxRateEntity;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 金蝶-财务模块-税率Service业务层处理
 * @author wutao
 * @date 2024-06-22
 */
@Service
public class KingBaseTaxRateServiceImpl extends ServiceImpl<KingBaseTaxRateMapper, KingBaseTaxRateEntity> implements IKingBaseTaxRateService {

    @Resource
    private IKingBaseTaxRateSystemService iKingBaseTaxRateSystemService;
    @Resource
    private IKingBaseTaxRateCategoryService iKingBaseTaxRateCategoryService;

    /**
     * 查询金蝶-财务模块-税率
     * @param id 金蝶-财务模块-税率ID
     * @return 金蝶-财务模块-税率
     */
    @Override
    public KingBaseTaxRateEntity selectKingBaseTaxRateEntityById (String id) {
        KingBaseTaxRateEntity baseTaxRateEntity = this.getById ( id );
        KingBaseTaxRateSystemEntity taxRateSystemEntity = iKingBaseTaxRateSystemService.getById ( baseTaxRateEntity.getTaxSystem () );
        if ( taxRateSystemEntity != null ) {
            baseTaxRateEntity.setTaxSystemName ( taxRateSystemEntity.getTitle () );
        }
        KingBaseTaxRateCategoryEntity taxRateCategoryEntity = iKingBaseTaxRateCategoryService.getById ( baseTaxRateEntity.getTaxCategory () );
        if ( taxRateCategoryEntity != null ) {
            baseTaxRateEntity.setTaxCategoryName ( taxRateCategoryEntity.getTitle () );
        }
        return baseTaxRateEntity;
    }

    /**
     * 查询金蝶-财务模块-税率列表
     * @param kingBaseTaxRateEntity 金蝶-财务模块-税率
     * @return 金蝶-财务模块-税率
     */
    @Override
    public Page selectKingBaseTaxRateEntityList (KingBaseTaxRateEntity kingBaseTaxRateEntity) {
        Page page = this.page ( new Page<> ( kingBaseTaxRateEntity.getPage (), kingBaseTaxRateEntity.getLimit () ), new LambdaQueryWrapper<KingBaseTaxRateEntity> ().eq ( KingBaseTaxRateEntity :: getIsUsed, ConstUtil.IS_USED_YES ) );
        List<KingBaseTaxRateEntity> records = page.getRecords ();
        records.forEach ( tax -> {
            KingBaseTaxRateSystemEntity taxRateSystemEntity = iKingBaseTaxRateSystemService.getById ( tax.getTaxSystem () );
            if ( taxRateSystemEntity != null ) {
                tax.setTaxSystemName ( taxRateSystemEntity.getTitle () );
            }
            KingBaseTaxRateCategoryEntity taxRateCategoryEntity = iKingBaseTaxRateCategoryService.getById ( tax.getTaxCategory () );
            if ( taxRateCategoryEntity != null ) {
                tax.setTaxCategoryName ( taxRateCategoryEntity.getTitle () );
            }
        } );
        page.setRecords ( records );
        return page;
    }

    /**
     * 新增金蝶-财务模块-税率
     * @param kingBaseTaxRateEntity 金蝶-财务模块-税率
     * @return 结果
     */
    @Override
    public AjaxResult insertKingBaseTaxRateEntity (KingBaseTaxRateEntity kingBaseTaxRateEntity) {
        kingBaseTaxRateEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        int count = this.count ();
        kingBaseTaxRateEntity.setRateCode ( "SL".concat ( String.format ( "%08d", count ) ) );
        kingBaseTaxRateEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseTaxRateEntity.setDeptId ( CurrentUserUtil.getCompanyId () );
        kingBaseTaxRateEntity.setCreateTime ( DateUtils.getNowDate () );
        boolean save = this.save ( kingBaseTaxRateEntity );
        if ( ! save ) {
            throw new CustomException ( "新增税率失败！" );
        }
        return AjaxResult.success ();
    }

    /**
     * 修改金蝶-财务模块-税率
     * @param kingBaseTaxRateEntity 金蝶-财务模块-税率
     * @return 结果
     */
    @Override
    public AjaxResult updateKingBaseTaxRateEntity (KingBaseTaxRateEntity kingBaseTaxRateEntity) {
        kingBaseTaxRateEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseTaxRateEntity.setUpdateTime ( DateUtils.getNowDate () );
        boolean b = this.updateById ( kingBaseTaxRateEntity );
        if ( ! b ) {
            throw new CustomException ( "修改税率失败！" );
        }
        return AjaxResult.success ();
    }

    /**
     * 批量删除金蝶-财务模块-税率
     * @param id 需要删除的金蝶-财务模块-税率ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingBaseTaxRateEntityByIds (String id) {
        List<KingBaseTaxRateEntity> list = this.list ( new LambdaQueryWrapper<KingBaseTaxRateEntity> ().eq ( KingBaseTaxRateEntity :: getId, id ) );
        List<String> idList = list.stream ().filter ( tax -> tax.getIsDel () == 1 ).map ( KingBaseTaxRateEntity :: getId ).collect ( Collectors.toList () );
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( tax -> {
            if ( tax.getIsDel () == 2 ) {
                stringBuilder.append ( tax.getTitle () );
            }
        } );
        stringBuilder.append ( "】" );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseTaxRateEntity> ().
                set ( KingBaseTaxRateEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingBaseTaxRateEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseTaxRateEntity :: getUpdateTime, DateUtils.getNowDate () ).
                in ( KingBaseTaxRateEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除税率失败！" );
        }
        String str = "删除税率成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中" ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success ( str );
    }

    /**
     * 删除金蝶-财务模块-税率信息
     * @param id 金蝶-财务模块-税率ID
     * @return 结果
     */
    @Override
    public AjaxResult disabled (String id) {
        KingBaseTaxRateEntity baseTaxRateEntity = this.getById ( id );
        if ( baseTaxRateEntity == null ) {
            throw new CustomException ( "无此税率！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseTaxRateEntity> ().
                set ( KingBaseTaxRateEntity :: getIsDisabled, baseTaxRateEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingBaseTaxRateEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseTaxRateEntity :: getUpdateTime, DateUtils.getNowDate () ).
                eq ( KingBaseTaxRateEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( baseTaxRateEntity.getIsDisabled () == 1 ? "禁用税率失败！" : "解禁税率失败" );
        }
        return AjaxResult.success ();
    }
}
